﻿/* Copyright (c) 2010 Lexalytics Inc.

 Permission is hereby granted, free of charge, to any person
 obtaining a copy of this software and associated documentation
 files (the "Software"), to deal in the Software without
 restriction, including without limitation the rights to use,
 copy, modify, merge, publish, distribute, sublicense, and/or sell
 copies of the Software, and to permit persons to whom the
 Software is furnished to do so, subject to the following
 conditions:

 The above copyright notice and this permission notice shall be
 included in all copies or substantial portions of the Software.

 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
 OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 OTHER DEALINGS IN THE SOFTWARE.
*/

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

// Needed for Office and Excel integration
using Excel = Microsoft.Office.Interop.Excel;

namespace Lexalytics4Excel.Dialogs
{
    /// <summary>
    /// Form to allow user to specify name for a results tab to create when one already exists
    /// </summary>
    public partial class ResultsSheetCreate : Form
    {
        private string _resultsType = "";
        private Excel.Workbook _book = null;

        /// <summary>
        /// String containing user entry of tab name
        /// </summary>
        public string TabName
        {
            get { return txtTabName.Text; }
        }

        /// <summary>
        /// Boolean indicating user choice to overwrite result tab
        /// </summary>
        public bool OverwriteTab
        {
            get { return optOverwrite.Checked; }
        }

        /// <summary>
        /// Default constructor
        /// </summary>
        /// <param name="resultsType">Indicator of result type with existing tab</param>
        /// <param name="currentWorkbook">Current workbook for checking user-entered tab name</param>
        public ResultsSheetCreate(string resultsType, Excel.Workbook currentWorkbook)
        {
            _resultsType = resultsType;
            _book = currentWorkbook;
            InitializeComponent();
        }

        private void ResultsSheetCreate_Load(object sender, EventArgs e)
        {
            this.Text = String.Format("\"{0}\" Tab Exists", _resultsType);
            DescriptionText.Text = DescriptionText.Text.Replace("<insert>", _resultsType);
            optOverwrite.Checked = true;
            txtTabName.Enabled = false;
            errTabName.SetError(txtTabName, "");
        }

        private void optOverwrite_CheckedChanged(object sender, EventArgs e)
        {
            if (optOverwrite.Checked)
            {
                errTabName.SetError(this.txtTabName, "");
                btnOK.Enabled = true;
            }
        }
        private void optCreate_CheckedChanged(object sender, EventArgs e)
        {
            txtTabName.Enabled = optCreate.Checked;
            txtTabName.Focus();
        }

        private void txtTabName_Validating(object sender, CancelEventArgs e)
        {
            if (optOverwrite.Checked)
            {
                errTabName.SetError(this.txtTabName, "");
            }
            else
            {
                if (ValidTabName())
                {
                    errTabName.SetError(this.txtTabName, "");
                }
                else
                {
                    errTabName.SetError(this.txtTabName, "Tab name required");
                }
            }
        }

        private bool ValidTabName()
        {
            bool bRet = true;
            string strTabName = txtTabName.Text;

            if ((strTabName.Trim().Length == 0) || (strTabName.Length > 30))
            {
                errTabName.SetError(txtTabName, "Tab name is required and must be less than 31 characters");
                bRet = false;
            }
            if (strTabName.IndexOfAny(new char[] { ' ', '!', '@', '#', '$', '%', '&', '(', ')', '+', '~', '`', '"', ':', ';', ',', '.', '|' }) > -1)
            {
                errTabName.SetError(txtTabName, "Tab name cannot contain special characters !@#$%&()+~`\"':;,.| ");
                bRet = false;
            }

            foreach (Excel.Worksheet ws in _book.Sheets)
            {
                if (ws.Name == strTabName)
                {
                    errTabName.SetError(txtTabName, "Tab name already exists.");
                    bRet = false;
                }
            }

            if (bRet)
                errTabName.SetError(txtTabName, "");

            return bRet;
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            if (ValidTabName() || optOverwrite.Checked)
            {
                this.DialogResult = DialogResult.OK;
                this.Close();
            }
        }

    }
}
